package org.cangku.controller;

import org.cangku.dao.LinShiDao;
import org.cangku.entity.Linshi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/linshi")
public class LinshiController {

    @Autowired
    private LinShiDao linShiDao;

    @RequestMapping("/query")
    @ResponseBody
    public Map<String,Object> query(int bh, String mc, int num, double dj, double money,String guige, String danhao){
        Map<String,Object> map = new HashMap<String, Object>();

        int i = linShiDao.cunzai(danhao,bh);
        Linshi ls = new Linshi();
        ls.setCid(bh);
        ls.setCname(mc);
        ls.setCnum(num);
        ls.setCdanjia(dj);
        ls.setCzongjia(money);
        ls.setCnumber(danhao);
        ls.setCguige(guige);
        if(i>0){
            linShiDao.updateNumAndMoney(ls);
        }else{
            linShiDao.inert(ls);
        }

        map.put("list",linShiDao.queryByDanhao(danhao));
        map.put("qian",linShiDao.queryZongQian(danhao));


        return map;
    }

    @RequestMapping("/danshan")
    @ResponseBody
    public Map<String,Object> deleteByid(String danhao,int id){
        Map<String,Object> map = new HashMap<String, Object>();

        linShiDao.deleteOne(id);
        int i = linShiDao.cunzaidanhao(danhao);
        if(i>0){
            map.put("zai",1);
            map.put("list",linShiDao.queryByDanhao(danhao));
            map.put("qian",linShiDao.queryZongQian(danhao));
        }else{
            map.put("zai",0);
        }


        return map;
    }

    @RequestMapping("/duoshan")
    @ResponseBody
    public Map<String,Object> deleteDanHao(String danhao){
        Map<String,Object> map = new HashMap<String, Object>();

        int i = linShiDao.deleteByDanhao(danhao);
        if(i>0){
            map.put("count",1);
        }else{
            map.put("count",0);
        }
        return map;
    }
}
